// pages/my_account_addr_edit/index.js
import WxValidate from '../../utils/wx-validate/WxValidate.js'

var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: null,
        name: '',
        mobile: '',
        regionConcat: '',
        addrdetail: '',
        region: []
    },
    bindRegionChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            region: e.detail.value,
            regionConcat: e.detail.value[0] + "，" + e.detail.value[1] + "，" + e.detail.value[2]
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        //表单校验
        const rules = {
            name: {
                required: true,
                maxlength: 20
            },
            mobile: {
                required: true,
                tel: true,
            },
            region: {
                required: true
            },
            addrDetail: {
                required: true
            }
        } 

        const messages = {
            name: {
                required: '请输入收件人姓名',
                maxlength: '名字不能超出20个字'
            },
            mobile: {
                required: '请输入手机号码',
                tel: '请输入正确的手机号码',
            },
            region: {
                required: '请选择省市区'
            },
            addrDetail: {
                required: '请填写详细地址'
            }
        }

        this.WxValidate = new WxValidate(rules, messages)



        //从缓存中获取到当前编辑的数据
        var editObj = wx.getStorageSync("my_account_addr_edit_obj");
        if (editObj) {
            this.setData({
                id: editObj.id,
                name: editObj.name,
                mobile: editObj.mobile,
                regionConcat: editObj.region,
                addrdetail: editObj.addrDetail,
                region: editObj.region.split("，")
            })
            //将页面标题设置为修改
            wx.setNavigationBarTitle({
                title: '修改地址信息'
            })
        }
    },
    submitForm: function(e) {

        console.log(e)
        var param = e.detail.value;
        //进行表单校验
        if (!this.WxValidate.checkForm(param)) {
            const error =this.WxValidate.errorList[0]
            //提示信息
            console.log(error);
            app.showModal(error);
            return false;
        }

        wx.showLoading();
        //提交表单数据
        
        //判断是添加，还是修改
        var submitUrl = "/wx/system/deliveryAddr/save";
        if (param.id) {
            submitUrl = "/wx/system/deliveryAddr/update"
        }
        //请求后台接口，保存数据
        // wx.request({
        //     url: app.server.addr + submitUrl,
        //     header: {
        //         'content-type': 'application/json',
        //         'sessionId': wx.getStorageSync('sessionId')
        //     },
        //     data: param,
        //     method: 'POST',
        //     success: function(res) {
        //         console.info("res = %o", res)
        //         // 返回上一页
        //         wx.navigateBack();
        //     },complete: function(){
        //         wx.hideLoading();
        //     }
        // })

        app.WxRequest
            .postRequest(submitUrl, {
                data: {
                    ...param,
                    loading: true
                },
                header: { 'content-type': 'application/json'}
            })
            .then(res => {
                wx.navigateBack();
            })

    },
    del: function(e) {
        //获取到需要删除的ID
        var id = this.data.id;
        
        //给予用户提示
        wx.showModal({
            title: '提示',
            content: '是否确认删除？',
            confirmText: "确认",
            cancelText: "取消",
            success: function(res) {
                console.log(res);
                //如果点击确定
                if (res.confirm) {
                    //封装需要更新的参数
                    var param = {
                        id: id,
                        isDeleted: '1'
                    }
                    wx.showLoading();
                    //发起后台请求
                    wx.request({
                        url: app.server.addr + '/wx/system/deliveryAddr/update',
                        method: 'POST',
                        data: param,
                        header: {
                            'content-type': 'application/json',
                            'sessionId': wx.getStorageSync('sessionId')
                        },
                        success: function(res) {
                            console.log(res)
                            wx.hideLoading();
                            // 返回上一页
                            wx.navigateBack();
                        }
                    })
                } else {
                    console.log('不删除了')
                }
            }
        });
    },
    onUnload: function() {
        //当用户离开编辑页面时，移除缓存中的数据
        console.info("移除缓存中的数据")
        wx.removeStorageSync("my_account_addr_edit_obj")
    }
})